package com.test;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class ListItem extends Item {
	public ListItem(JsonKey key,Object o){
		super(key,o);
	}
	
	protected void doReady(JsonWriter w){
		List list = (List)obj;		
		Class c = obj.getClass();
	
		if(!this.findAction(w, c)){
			return  ;
		}
		List<Item> items = makeItems(list,c);
		
		if(this.childrenAction!=null){
			for(ChildrenAction a:this.childrenAction){
				if(!a.doIt(w, key, items)){
					return ;
				}
			}
		}
		
		Context ctx = w.ctx;
		RenderStack stack = ctx.getObjectStack();
		for(Object item:items){
			stack.push(item);
		}
		w.writeKey(key);
		w.enterArray();
		w.getCtx().scops.peek().addAllRuleNode(newRules);
	

		
	}
	
	protected void doRender(JsonWriter w){
		w.exitArray();
	}
	

	
	private List<Item>  makeItems(List list,Class c){
		List<Item> newList = new LinkedList<Item> ();
		int i = 0;
		ListIterator it = list.listIterator(list.size());
		while(it.hasPrevious()){
			JsonKey key = new JsonKey(i,true);
			Item item = ItemFactory.create(key, c, it.previous());
			item.setParent(this);
			newList.add(item);
			i++;
		}
	
		return newList;
	}
}
